package com.download.backstage.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.download.backstage.entity.Dictionary;
import com.download.web.entity.BusDicDictionary;
import com.download.web.entity.BusDicItem;
import com.download.web.entity.model.Page;
import com.download.web.service.IBusDicDictionaryService;
import com.download.web.service.IBusDicItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @description:
 * @author: cmy
 * @create: 2019-02-24 16:56
 **/
@RestController
@RequestMapping("/download/item")
public class ItemController {
    @Autowired
    private IBusDicItemService itemService;
    @Autowired
    private IBusDicDictionaryService dicDictionaryService;


    @RequestMapping("/itemList")
    public List<BusDicItem> itemList(Integer dictionaryId){
        return itemService.itemsByDictId(dictionaryId);
    }

    @RequestMapping("/dictionaryList")
    public Page<Dictionary> dictionaries(Integer pageIndex,Integer pageSize){
        return itemService.dictionaryPage(pageIndex,pageSize);
    }

    /**
     * 按字典id查找字典信息
     * @param dictId
     * @return
     */
    @RequestMapping("/findDictionaryById")
    public Dictionary findDictionaryById(Integer dictId){
        return new Dictionary(dicDictionaryService.getOne(new QueryWrapper<BusDicDictionary>().eq("id",dictId)).getValue(),itemService.itemValues(dictId));
    }

    /**
     * 新增字典
     * @param dictionary
     */
    @RequestMapping("/dictionaryAdd")
    public void dictionaryAdd(@RequestBody Dictionary dictionary){
        BusDicDictionary dicDictionary=new BusDicDictionary(dictionary.getValue());
        dicDictionaryService.save(dicDictionary);
        List<BusDicItem> items=dictionary.getItems().stream().map(i->new BusDicItem(i,dicDictionary.getId())).collect(Collectors.toList());
        itemService.saveBatch(items);
    }

    /**
     * 修改字典
     * @param dictionary
     */
    @RequestMapping("/dictionaryUpdate")
    public void dictionaryUpdate(@RequestBody Dictionary dictionary){
        BusDicDictionary dicDictionary=new BusDicDictionary(dictionary.getId(),dictionary.getValue());
        dicDictionaryService.updateById(dicDictionary);
        itemService.remove(new QueryWrapper<BusDicItem>().eq("dictionary_id",dicDictionary.getId()));
        List<BusDicItem> items=dictionary.getItems().stream().map(i->new BusDicItem(i,dicDictionary.getId())).collect(Collectors.toList());
        itemService.saveBatch(items);
    }

    /**
     * 删除字典
     * @param dicId
     */
    @RequestMapping("/dictionaryDel")
    public void dictionaryDel(Integer dicId){
        dicDictionaryService.removeById(dicId);
        itemService.remove(new QueryWrapper<BusDicItem>().eq("dictionary_id",dicId));
    }


}
